package com.lenovo.tool;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/*
 * 根据miss的目录树将北京在线库中的翻译分离出来
 */
public class TranslationExtract {

	public static void main(String[] args) {
		if (args.length != 1) {
            System.out.println("please enter the config file name");
            return;
        }

        File configFile = new File(args[0]);
        if (!configFile.exists() || !configFile.isFile()) {
            System.out.println("config file error");
            return;
        }
        
        try {
			Document doc = DocumentUtils.openDocument(configFile);
			String baseStr = doc.getElementsByTagName("basedir").item(0).getTextContent().trim();
			String importStr = doc.getElementsByTagName("importDir").item(0).getTextContent().trim();
			String outStr = doc.getElementsByTagName("outDir").item(0).getTextContent().trim();
			File baseDir = new File(baseStr);
			File importDir = new File(importStr);
			File outDir = new File(outStr);
			importAction(baseDir, importDir, outDir);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void importAction(File baseDir, File importDir, File outDir) {
		File[] childsBase = baseDir.listFiles();
		File fileBase;
		File fileImport;
		File fileOut;
		for (int i = 0; i  < childsBase.length; i++) {
			fileBase = childsBase[i];
			System.out.println(fileBase.getAbsolutePath());
			if(fileBase.isDirectory()) {
				fileImport = new File(importDir, fileBase.getName());
				if(fileImport.exists()) {
					fileOut = new File(outDir, fileBase.getName());
					fileOut.mkdir();
					importAction(fileBase, fileImport, fileOut);
				}
			} else {
				fileImport = new File(importDir, "strings.xml");
				if(fileImport.exists()) {
					importFile(fileBase, fileImport, outDir);
				}
			}
		}
	}

	private static void importFile(File fileBase, File fileImport, File outDir) {
		try {
			Document docBase = DocumentUtils.openDocument(fileBase);
			Document docImport = DocumentUtils.openDocument(fileImport);
			Document docOut = DocumentUtils.createDocument();
			Element root = docOut.createElement("resources");
			docOut.appendChild(root);
			
			NodeList stringBaseList = docBase.getElementsByTagName("string");
			NodeList stringImportList = docImport.getElementsByTagName("string");
			Element nodeBase;
			Element nodeImport;
			for (int i = 0; i < stringBaseList.getLength(); i++) {
				nodeBase = (Element) stringBaseList.item(i);
				for (int j = 0; j < stringImportList.getLength(); j++) {
					nodeImport = (Element) stringImportList.item(j);
					if(nodeImport.getAttribute("name").equals(nodeBase.getAttribute("name"))) {
						root.appendChild(docOut.importNode(nodeImport, true));
					}
				}
			}
			NodeList arrayBaseList = docBase.getElementsByTagName("string-array");
			NodeList arrayImportList = docBase.getElementsByTagName("string-array");
			for (int i = 0; i < arrayBaseList.getLength(); i++) {
				nodeBase = (Element) arrayBaseList.item(i);
				for (int j = 0; j < arrayImportList.getLength(); j++) {
					nodeImport = (Element) arrayImportList.item(j);
					if(nodeImport.getAttribute("name").equals(nodeBase.getAttribute("name"))) {
						root.appendChild(docOut.importNode(nodeImport, true));
					}
				}
			}
			if(!root.hasChildNodes()) {
				return;
			}
			DocumentUtils.writeDocument(docOut, new File(outDir, fileBase.getName()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

}
